package com.dreamcode.caloriestracker.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dreamcode.caloriestracker.collectors.ProfileDAO;
import com.dreamcode.caloriestracker.collectors.ProfileDAOimpl;
import com.dreamcode.caloriestracker.collectors.SportListAdapter;
import com.dreamcode.caloriestracker.modelclass.ProfileDetails;
import com.dreamcode.caloriestracker.utils.CalorieUtils;

public class CaloriesCompareActivity extends Activity {

	private Spinner sportSpinner1 = null;
	private Spinner sportSpinner2 = null;
	private Button calculate = null;
	private Button back = null;
	private RadioButton radio_calories = null;
	private RadioButton radio_time = null;
	private EditText calculateCal = null;
	private TextView textView = null;
	private ProfileDAO profileDAO = null;
	private ProfileDetails profileDetails = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.caloriescompare);
		initView();
	}

	private void initView() {
		profileDAO = ProfileDAOimpl.getInstance();
		profileDetails = profileDAO.getProfileDetails(
				CaloriesCompareActivity.this, 0);
		textView = (TextView) findViewById(R.id.value_check);
		calculateCal = (EditText) findViewById(R.id.calculate_cal_min);
		radio_calories = (RadioButton) findViewById(R.id.compare_radio_calories);
		radio_calories.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				textView.setText(" min");
				Toast.makeText(CaloriesCompareActivity.this,
						"Please fill time in minutes in the field ",
						Toast.LENGTH_SHORT).show();
			}

		});

		radio_time = (RadioButton) findViewById(R.id.compare_radio_time);
		radio_time.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				textView.setText(" cal");
				Toast.makeText(CaloriesCompareActivity.this,
						"Please fill calories in the field ",
						Toast.LENGTH_SHORT).show();
			}

		});

		sportSpinner1 = (Spinner) findViewById(R.id.sport_selector_1);
		sportSpinner1.setAdapter(new SportListAdapter(
				CaloriesCompareActivity.this, CalorieUtils.getSports()));

		sportSpinner2 = (Spinner) findViewById(R.id.sport_selector_2);
		sportSpinner2.setAdapter(new SportListAdapter(
				CaloriesCompareActivity.this, CalorieUtils.getSports()));

		calculate = (Button) findViewById(R.id.btn_calculate);
		calculate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calculate();
			}

		});
		back = (Button) findViewById(R.id.btn_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}

		});
	}

	private void calculate() {
		if (calculateCal.getText().toString() != null
				&& !calculateCal.getText().toString().equals("")) {
			if (radio_time.isChecked()) {
				String calculateTimeFromSport1 = CalorieUtils
						.calculateTimeByCalories(
								sportSpinner1.getSelectedView().getId(),
								Double
										.parseDouble(profileDetails
												.getWeParam().equals("kg") ? profileDetails
												.getProfileWeight()
												: String
														.valueOf(Math
																.round(Double
																		.parseDouble(profileDetails
																				.getProfileWeight()) / 2.2))),
								Integer.parseInt(calculateCal.getText()
										.toString()));
				String calculateTimeFromSport2 = CalorieUtils
						.calculateTimeByCalories(
								sportSpinner2.getSelectedView().getId(),
								Double
										.parseDouble(profileDetails
												.getWeParam().equals("kg") ? profileDetails
												.getProfileWeight()
												: String
														.valueOf(Math
																.round(Double
																		.parseDouble(profileDetails
																				.getProfileWeight()) / 2.2))),
								Integer.parseInt(calculateCal.getText()
										.toString()));

				AlertDialog groupDialog = new AlertDialog.Builder(
						CaloriesCompareActivity.this).setTitle(
						R.string.calculation).setMessage(
						"To burn " + calculateCal.getText().toString()
								+ "cal with "
								+ sportSpinner1.getSelectedItem().toString()
								+ " you need " + calculateTimeFromSport1
								+ " minutes and with the second sport "
								+ sportSpinner2.getSelectedItem().toString()
								+ " you need " + calculateTimeFromSport2
								+ " minutes").setPositiveButton(R.string.ok,
						null).show();
			} else if (radio_calories.isChecked()) {
				String calculateCaloriesFromSport1 = CalorieUtils
						.calculateCaloriesByTime(
								sportSpinner1.getSelectedView().getId(),
								Double
										.parseDouble(profileDetails
												.getWeParam().equals("kg") ? profileDetails
												.getProfileWeight()
												: String
														.valueOf(Math
																.round(Double
																		.parseDouble(profileDetails
																				.getProfileWeight()) / 2.2))),
								Integer.parseInt(calculateCal.getText()
										.toString()));
				String calculateCaloriesFromSport2 = CalorieUtils
						.calculateCaloriesByTime(
								sportSpinner2.getSelectedView().getId(),
								Double
										.parseDouble(profileDetails
												.getWeParam().equals("kg") ? profileDetails
												.getProfileWeight()
												: String
														.valueOf(Math
																.round(Double
																		.parseDouble(profileDetails
																				.getProfileWeight()) / 2.2))),
								Integer.parseInt(calculateCal.getText()
										.toString()));

				AlertDialog groupDialog = new AlertDialog.Builder(
						CaloriesCompareActivity.this).setTitle(
						R.string.calculation).setMessage(
						"For " + calculateCal.getText().toString()
								+ "min with "
								+ sportSpinner1.getSelectedItem().toString()
								+ "you will burn "
								+ calculateCaloriesFromSport1 + " calories "
								+ "and with "
								+ sportSpinner2.getSelectedItem().toString()
								+ " you will burn "
								+ calculateCaloriesFromSport2 + " calories")
						.setPositiveButton(R.string.ok, null).show();
			} else {

			}
		} else {

		}

	}
}
